<?php
header('Content-type: text/plain');

require_once('frontstart.inc.php');   # defines: $options[], $context

handle_request($options);

function handle_request ($options) {
    try {
        $table            = $_REQUEST['table'];
        $id_column        = $_REQUEST['id_column'];
        $parent_id_column = $_REQUEST['parent_id_column'];
        $text_column      = $_REQUEST['text_column'];
        $parent_id_value  = $_REQUEST['node'];
        $neat             = $_REQUEST['neat'];

        if (!isset($id_column)) {
            throw new Exception("The id_column parameter must be supplied");
        }
        #if (!isset($parent_id_column)) {
        #    throw new Exception("The parent_id_column parameter must be supplied");
        #}
        if (!isset($text_column)) {
            throw new Exception("The text_column parameter must be supplied");
        }
        $columns = array($id_column, $text_column);

        $context = App::context($options);
        $database = $context->database();

        if (! $columns) {
            $columns = null;
        }
        elseif (!is_array($columns)) {
            $columns = preg_split('/,/', $columns);
        }

        $params  = array();
        $matches = array();
        if ($parent_id_column && isset($parent_id_value)) {
            $params["$parent_id_column-eq"] = $parent_id_value;
        }
        foreach ($_REQUEST as $key => $value) {
            if (preg_match('/^p-(.+)$/', $key, &$matches)) {
                $params[$matches[1]] = $value;
            }
        }

        $alias_columns = array('id','text','parent');
        $get_options = array(
            'column_defs' => array()
        );
        for ($i = 0; $i < sizeof($columns); $i++) {
            $column = $columns[$i];
            $alias_column = $alias_columns[$i];
            $get_options['column_defs'][$column] = array('alias' => $alias_column);
        }

        if (isset($_REQUEST['sort']) && $_REQUEST['sort']) {
            $order_by_columns = preg_split('/,/',$_REQUEST['sort']);
            if (isset($_REQUEST['dir']) && $_REQUEST['dir']) {
                $order_by_dirs = preg_split('/,/',$_REQUEST['dir']);
                for ($i = 0; $i < sizeof($order_by_columns); $i++) {
                    $order_by_columns[$i] .= '.' . $order_by_dirs[$i];
                }
            }
            $get_options['order_by'] = $order_by_columns;
        }
        if (isset($_REQUEST['start'])) {
            $get_options['startrow'] = $_REQUEST['start'];
        }
        if (isset($_REQUEST['limit'])) {
            $get_options['numrows'] = $_REQUEST['limit'];
        }
        if (isset($_REQUEST['debug_sql'])) {
            $get_options['debug_sql'] = $_REQUEST['debug_sql'];
        }

        $rows = $database->get_hashes($table, $params, $columns, $get_options);
        if (!$rows) {
            throw new Exception("No data");
        }

        $data = array();
        for ($i = 0; $i < sizeof($rows); $i++) {
            $data[$i] = array();
            $data[$i]['id'] = $rows[$i][$id_column];
            $data[$i]['text'] = $rows[$i][$text_column];
            $data[$i]['leaf'] = true;
        }

        $json = json_encode($data);
        if ($neat) {
            $json = preg_replace('/\\},\\{/', "},\n {", $json);
            $json = preg_replace('/\\],\\[/', "],\n [", $json);
        }
        echo $json . "\n";
    }
    catch (Exception $e) {
        return_json_error($e->getMessage());
    }
    return(null);
}

function return_json_error ($message) {
    if (preg_match("/\n/",$message)) {
        $message = preg_replace("/ *\n */"," ",$message);
    }
    if (preg_match("/\\'/",$message)) {
        $message = preg_replace("/\\'/","\\'",$message);
    }
    echo "{\n";
    echo "    'success': false,\n";
    echo "    'msg': '$message'\n";
    echo "}\n";
}

